package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.AddressBean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.YuShouDetailsBean;
import com.wnhz.work.yz.bean.YuShouPayBean;
import com.wnhz.work.yz.view.SwitchView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.x;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class YuShouPayActivity extends BaseActivity implements View.OnClickListener, SwitchView.OnStateChangedListener {
    private RecyclerView recycler;
    private TextView total_fee, need_pay;
    private LinearLayout ll_recode, ll_contract, ll_switch, ll_invoice, ll_address;
    private TextView tv_recode, tv_contract, tv_invoice, tv_address, order_num, order_time, order_person;
    private EditText ed_note;
    private SwitchView switchButton;
    private TextView line, toast_img;
    private int count = 0;

    private ArrayList<YuShouDetailsBean.InfoEntity> packageX = new ArrayList<>();
    private String max_type;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yu_shou_pay);
        EventBus.getDefault().register(this);
        initData();
        initTitle();
        initView();
    }


    private void initData() {
        packageX = (ArrayList<YuShouDetailsBean.InfoEntity>) getIntent().getSerializableExtra("bean");
        if (packageX == null) {
            packageX = new ArrayList<>();
        }
        max_type = getIntent().getStringExtra("type");
        infoBean = (YuShouPayBean.InfoEntity) getIntent().getSerializableExtra("infoBean");
    }

    private void initView() {
        order_person = (TextView) findViewById(R.id.order_person);
        order_num = (TextView) findViewById(R.id.order_num);
        order_time = (TextView) findViewById(R.id.order_time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        String nowTime = format.format(new Date());
        order_time.setText(nowTime + "");
        toast_img = (TextView) findViewById(R.id.toast_img);
        ll_recode = (LinearLayout) findViewById(R.id.ll_recode);
        ll_contract = (LinearLayout) findViewById(R.id.ll_contract);
        ll_switch = (LinearLayout) findViewById(R.id.ll_switch);
        ll_invoice = (LinearLayout) findViewById(R.id.ll_invoice);
        ll_address = (LinearLayout) findViewById(R.id.ll_address);
        ll_recode.setOnClickListener(this);
        ll_contract.setOnClickListener(this);
        ll_invoice.setOnClickListener(this);
        ll_address.setOnClickListener(this);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_recode = (TextView) findViewById(R.id.tv_recode);
        tv_contract = (TextView) findViewById(R.id.tv_contract);
        tv_invoice = (TextView) findViewById(R.id.tv_invoice);
        ed_note = (EditText) findViewById(R.id.ed_note);
        switchButton = (SwitchView) findViewById(R.id.switchButton);
        switchButton.setOnStateChangedListener(this);
        line = (TextView) findViewById(R.id.line);

        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        total_fee = (TextView) findViewById(R.id.total_fee);
        need_pay = (TextView) findViewById(R.id.need_pay);
        need_pay.setOnClickListener(this);
        setData();
    }

    private YuShouPayBean.InfoEntity infoBean = new YuShouPayBean.InfoEntity();


    private void setData() {
        DecimalFormat df = new DecimalFormat("######0.00");
        need_pay.setText("￥" + infoBean.getPay_money());
        double to = 0d;
        for (int i = 0; i < packageX.size(); i++) {
            to += Double.parseDouble(packageX.get(i).getGoods_num()) * Double.parseDouble(packageX.get(i).getGoods_price());
        }
        total_fee.setText("￥" + df.format(to));
        order_num.setText(infoBean.getBudget_no() + "");
        order_person.setText(MyApplication.getInstance().getUserBean().getUserName() + "");
        recycler.setAdapter(new BaseRVAdapter(this, packageX) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yushou_pay;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                x.image().bind(holder.getImageView(R.id.shop_img), packageX.get(position).getGoods_img());
                holder.setTextView(R.id.title, packageX.get(position).getGoods_name() + "");
                holder.setTextView(R.id.sku, packageX.get(position).getCate_name() + "");
                holder.setTextView(R.id.desc, packageX.get(position).getGoods_sku_info());
                holder.setTextView(R.id.price, "￥" + packageX.get(position).getGoods_price());
                holder.setTextView(R.id.nums, "×" + packageX.get(position).getGoods_num());
            }
        });
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("预售支付");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        submit.setText("支付");
        submit.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                startActivityForResult(new Intent(YuShouPayActivity.this, YuShouGotoPayActivity.class)
                        .putExtra("pay_money", infoBean.getPay_money())
                        .putExtra("budget_no", infoBean.getBudget_no())
                        .putExtra("wallet", infoBean.getWallet())
                        .putExtra("type", max_type), ISBLACK
                );
                break;
            //感叹号
            case R.id.need_pay:
                toast_img.setText("预付" + infoBean.getPay_money() + "金额,剩余金额提货时支付;违反规则不退回所付金额！");
                if (toast_img.getVisibility() == View.VISIBLE) {
                    toast_img.setVisibility(View.GONE);
                } else {
                    toast_img.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ll_recode:
                intent = new Intent(this, AddRecordsCodeActivity.class);
                startActivityForResult(intent, RECODE);
                break;
            case R.id.ll_contract:
              /*  intent = new Intent(this, StandardCompactActivity.class);
                intent.putExtra("url", infoBean.getContract().getUrl());
                intent.putExtra("msg", infoBean.getContract().getMsg());
                startActivityForResult(intent, CONTRACT);*/
                break;

            case R.id.ll_invoice:
                intent = new Intent(this, InvoiceActivity.class);
                startActivityForResult(intent, INVOICE);
                break;
            case R.id.ll_address:
                intent = new Intent(this, ChooseAddressActivity.class);
                startActivityForResult(intent, ADDRESS);
                break;
        }
    }


    @Override
    public void toggleToOn(View view) {
        switch (view.getId()) {
            //发票信息
            case R.id.switchButton:
                switchButton.setOpened(true);
                showOrHidePickLayout3(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void toggleToOff(View view) {
        switch (view.getId()) {
            case R.id.switchButton:
                switchButton.setOpened(false);
                showOrHidePickLayout3(false);
                break;
            default:
                break;
        }
    }

    private boolean isNeed;

    private void showOrHidePickLayout3(boolean isNeedToPick) {
        isNeed = isNeedToPick;
        if (isNeedToPick) {
            ll_invoice.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
        } else {
            ll_invoice.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
    }

    private static final int RECODE = 991;
    private static final int CONTRACT = 992;
    //    private static final int SWITCH=993;
    private static final int INVOICE = 994;
    private static final int ADDRESS = 995;

    //备案
    private int recordsId;
    //发票
    private String receiptAddrId;
    private String invoiceType;
    private String invoiceTitle;
    private String company;
    private String receiptType;
    private String type;
    private String taxpayerId;
    private String bank;
    private String bankAccount;
    private String address;
    private String phone;
    private String getInvoiceResult;
    //地址
    private String addrId;

    private static final int VALUE_TAX = 895;
    private static final int ISBLACK = 893;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK || resultCode == VALUE_TAX) {
            switch (requestCode) {
                case RECODE:
                    recordsId = Integer.parseInt(data.getStringExtra("recordsId"));
                    String name = data.getStringExtra("recordsName");
                    tv_recode.setText(name + "");
                    break;
                case CONTRACT:
                    String pact = data.getStringExtra("pact");
                    if ("1".equals(pact)) {
                        tv_contract.setText("自定义合同");
                    }
                    break;
                case INVOICE:
                    if (resultCode == RESULT_OK) {
                        if (null != data) {
                            getInvoiceResult = data.getStringExtra("InvoiceResult");
                            receiptAddrId = data.getStringExtra("receiptAddrId");
                            invoiceType = data.getStringExtra("invoiceType");
                            invoiceTitle = data.getStringExtra("invoiceTitle");
                            company = data.getStringExtra("company");
                            receiptType = data.getStringExtra("receiptType");
                            type = data.getStringExtra("type");
                            tv_invoice.setText(getInvoiceResult);
                        }
                    } else if (resultCode == VALUE_TAX) {
                        if (data != null) {
                            getInvoiceResult = data.getStringExtra("InvoiceResult");
                            invoiceTitle = data.getStringExtra("invoiceTitle");
                            taxpayerId = data.getStringExtra("taxpayerId");
                            bank = data.getStringExtra("bank");
                            bankAccount = data.getStringExtra("bankAccount");
                            address = data.getStringExtra("address");
                            phone = data.getStringExtra("phone");
                            type = data.getStringExtra("type1");
                            tv_invoice.setText(getInvoiceResult);
                        }
                    }
                    break;
                case ADDRESS:
                    AddressBean addressBean = (AddressBean) data.getSerializableExtra("addressBean");
                    String province = addressBean.getProvince();
                    String city = addressBean.getCity();
                    String district = addressBean.getDistrict();
                    String place = addressBean.getPlace();
                    String addr = province + city + district + place;
                    tv_address.setText(addr);
                    addrId = addressBean.getAddress_id();
                    break;
                case ISBLACK:
                    break;

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
